/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormularioCliente.java
 *
 * Created on 23/06/2010, 09:32:39 PM
 */

package CapaGrafica;
import CapaDatos.Fecha;
import java.awt.Color;
import java.util.Arrays;
import javax.swing.JOptionPane;
import CapaLogica.ComunicacionServidor;
/**
 *
 * @author Maria
 */
public class FormularioCliente extends javax.swing.JFrame {
    private InicioSesion ventanaLogin;
    /** Creates new form FormularioCliente */
    public FormularioCliente(InicioSesion ventanaMadre) {
        initComponents();
        llenarComboBox();
        ventanaLogin = ventanaMadre;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBingresar = new javax.swing.JButton();
        jBcancelar = new javax.swing.JButton();
        jLnombre = new javax.swing.JLabel();
        jTFnombre = new javax.swing.JTextField();
        jLapellido = new javax.swing.JLabel();
        jTFapellido = new javax.swing.JTextField();
        jLcedula = new javax.swing.JLabel();
        jTFcedula = new javax.swing.JTextField();
        jLsexo = new javax.swing.JLabel();
        jCBsexo = new javax.swing.JComboBox();
        jLfechaNacimiento = new javax.swing.JLabel();
        jCBdia = new javax.swing.JComboBox();
        jCBmes = new javax.swing.JComboBox();
        jCBano = new javax.swing.JComboBox();
        jLestadoCivil = new javax.swing.JLabel();
        jCBestadoCivil = new javax.swing.JComboBox();
        jLpais = new javax.swing.JLabel();
        jCBpais = new javax.swing.JComboBox();
        jLlogin = new javax.swing.JLabel();
        jTFlogin = new javax.swing.JTextField();
        jLclave1 = new javax.swing.JLabel();
        jPFclave1 = new javax.swing.JPasswordField();
        jLclave2 = new javax.swing.JLabel();
        jPFclave2 = new javax.swing.JPasswordField();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jBingresar.setText("Agregar");
        jBingresar.setName("jBingresar"); // NOI18N
        jBingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBingresarActionPerformed(evt);
            }
        });

        jBcancelar.setText("Cancelar");
        jBcancelar.setName("jBcancelar"); // NOI18N
        jBcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcancelarActionPerformed(evt);
            }
        });

        jLnombre.setText("Nombre");
        jLnombre.setName("jLnombre"); // NOI18N

        jTFnombre.setName("jTFnombre"); // NOI18N

        jLapellido.setText("Apellido");
        jLapellido.setName("jLapellido"); // NOI18N

        jTFapellido.setName("jTFapellido"); // NOI18N

        jLcedula.setText("Cedula");
        jLcedula.setName("jLcedula"); // NOI18N

        jTFcedula.setName("jTFcedula"); // NOI18N

        jLsexo.setText("Sexo:");
        jLsexo.setName("jLsexo"); // NOI18N

        jCBsexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleccione>", "Masculino", "Femenino" }));
        jCBsexo.setName("jCBsexo"); // NOI18N

        jLfechaNacimiento.setText("Fecha nacimiento");
        jLfechaNacimiento.setName("jLfechaNacimiento"); // NOI18N

        jCBdia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dia" }));
        jCBdia.setName("jCBdia"); // NOI18N

        jCBmes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mes" }));
        jCBmes.setName("jCBmes"); // NOI18N
        jCBmes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBmesActionPerformed(evt);
            }
        });

        jCBano.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ano" }));
        jCBano.setName("jCBano"); // NOI18N
        jCBano.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBanoActionPerformed(evt);
            }
        });

        jLestadoCivil.setText("Estado civil");
        jLestadoCivil.setName("jLestadoCivil"); // NOI18N

        jCBestadoCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleccione>", "Soltero", "Casado", "Viudo", "Divorciado" }));
        jCBestadoCivil.setName("jCBestadoCivil"); // NOI18N

        jLpais.setText("Pais de origen");
        jLpais.setName("jLpais"); // NOI18N

        jCBpais.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Seleccione>", "Alemania", "Venezuela" }));
        jCBpais.setName("jCBpais"); // NOI18N

        jLlogin.setText("Nombre de usuario");
        jLlogin.setName("jLlogin"); // NOI18N

        jTFlogin.setName("jTFlogin"); // NOI18N

        jLclave1.setText("Clave");
        jLclave1.setName("jLclave1"); // NOI18N

        jPFclave1.setName("jPFclave1"); // NOI18N

        jLclave2.setText("Confirme clave");
        jLclave2.setName("jLclave2"); // NOI18N

        jPFclave2.setName("jPFclave2"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLclave2)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLclave1)
                            .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLlogin)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLpais)
                                    .addContainerGap())
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLestadoCivil)
                                        .addContainerGap())
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLfechaNacimiento)
                                            .addContainerGap())
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLcedula)
                                                    .addGap(75, 75, 75)
                                                    .addComponent(jTFcedula, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLapellido)
                                                    .addGap(71, 71, 71)
                                                    .addComponent(jTFapellido, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLnombre)
                                                    .addGap(71, 71, 71)
                                                    .addComponent(jTFnombre, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                    .addComponent(jBingresar)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 150, Short.MAX_VALUE)
                                                    .addComponent(jBcancelar))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLsexo)
                                                    .addGap(80, 80, 80)
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jCBsexo, 0, 188, Short.MAX_VALUE)
                                                        .addComponent(jCBpais, 0, 188, Short.MAX_VALUE)
                                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                            .addComponent(jCBdia, 0, 55, Short.MAX_VALUE)
                                                            .addGap(18, 18, 18)
                                                            .addComponent(jCBmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addGap(18, 18, 18)
                                                            .addComponent(jCBano, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addComponent(jCBestadoCivil, 0, 188, Short.MAX_VALUE)
                                                        .addComponent(jTFlogin, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                            .addComponent(jPFclave1, javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addComponent(jPFclave2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)))))
                                            .addGap(46, 46, 46)))))))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLnombre)
                    .addComponent(jTFnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLapellido)
                    .addComponent(jTFapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLcedula)
                    .addComponent(jTFcedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLsexo)
                    .addComponent(jCBsexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLfechaNacimiento)
                    .addComponent(jCBdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLestadoCivil)
                    .addComponent(jCBestadoCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLpais)
                    .addComponent(jCBpais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLlogin)
                    .addComponent(jTFlogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLclave1)
                    .addComponent(jPFclave1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLclave2)
                    .addComponent(jPFclave2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBcancelar)
                    .addComponent(jBingresar))
                .addGap(38, 38, 38))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBingresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBingresarActionPerformed
        char sexo                = ((String) this.jCBsexo.getSelectedItem()).charAt(0);
        char estadoCivil         = ((String) this.jCBestadoCivil.getSelectedItem()).charAt(0);
        String nacionalidad      = (String) this.jCBpais.getSelectedItem();
        String fechaNacimiento   = (String) this.jCBdia.getSelectedItem() + "/" + this.jCBmes.getSelectedItem() + "/" + this.jCBano.getSelectedItem();
        String mensajeClaves     = "Las contraseñas que ingresó no coinciden, por favor intente de nuevo";
        String mensajeRegistrado = "A continuación inicie sesión para comenzar a disfrutar de los servicios del sistema iResort";

        if (Arrays.equals(jPFclave1.getPassword(), jPFclave2.getPassword()))
        {
            String[] resultadoRegistro = ComunicacionServidor.registrarUsuario
                (
                    jTFnombre.getText(), jTFapellido.getText(), jTFcedula.getText(),
                    sexo, fechaNacimiento, estadoCivil, nacionalidad,
                    jTFlogin.getText(), jPFclave1.getPassword()
                ).split(":");

            if (resultadoRegistro[0].equals("TRUE"))
            {
                JOptionPane.showMessageDialog (this, mensajeRegistrado);
                ventanaLogin.setVisible(true);
                this.dispose();
            }
            else
            {
                JOptionPane.showMessageDialog (this, resultadoRegistro[1]);
                this.jPFclave1.setText(null);
                this.jPFclave2.setText(null);
            }
        }
        else
        {
            JOptionPane.showMessageDialog (this, mensajeClaves);
            this.jPFclave1.setText(null);
            this.jPFclave2.setText(null);
        } // TODO add your handling code here:
}//GEN-LAST:event_jBingresarActionPerformed

    private void jBcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcancelarActionPerformed
        ventanaLogin.setVisible(true);
        this.dispose();// TODO add your handling code here:
}//GEN-LAST:event_jBcancelarActionPerformed

    private void jCBmesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBmesActionPerformed
       if (!jCBmes.getSelectedItem().equals("Mes"))
            actualizarDias(); // TODO add your handling code here:
    }//GEN-LAST:event_jCBmesActionPerformed

    private void jCBanoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBanoActionPerformed
      if (!jCBmes.getSelectedItem().equals("Mes"))
           actualizarDias();  // TODO add your handling code here:
    }//GEN-LAST:event_jCBanoActionPerformed

    /**
    * @param args the command line arguments
    */
    private void actualizarDias() {
        int mes, año, i, l, maxDia = 31;

        mes = Integer.parseInt((String) jCBmes.getSelectedItem());
        l = jCBdia.getItemCount() - 1;

        if (mes == 2)
        {
            maxDia = 29;
            if (!jCBano.getSelectedItem().equals("Año"))
            {
                año = Integer.parseInt((String) jCBano.getSelectedItem());
                if (año % 400 != 0 && ((año % 4 != 0) || (año % 100 == 0)))
                    maxDia--;
            }
        }
        else if ((mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))
            maxDia = 30;

        if (maxDia < l)
            for (i = l; i > maxDia; i--)
                jCBdia.removeItemAt(i);

        if (maxDia > l)
            for (i = l; i < maxDia; i++)
                jCBdia.addItem (String.valueOf(i + 1));
    }
    private void llenarComboBox() {
        Integer i;
        Fecha fechaActual = new Fecha();
        int año = fechaActual.get(Fecha.YEAR);

        for (i = 1; i <= 31; i++)
            jCBdia.addItem(i.toString());

        for (i = 1; i <= 12; i++)
            jCBmes.addItem(i.toString());

        for (i = año - 100; i < año; i++)
            jCBano.addItem(i.toString());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBcancelar;
    private javax.swing.JButton jBingresar;
    private javax.swing.JComboBox jCBano;
    private javax.swing.JComboBox jCBdia;
    private javax.swing.JComboBox jCBestadoCivil;
    private javax.swing.JComboBox jCBmes;
    private javax.swing.JComboBox jCBpais;
    private javax.swing.JComboBox jCBsexo;
    private javax.swing.JLabel jLapellido;
    private javax.swing.JLabel jLcedula;
    private javax.swing.JLabel jLclave1;
    private javax.swing.JLabel jLclave2;
    private javax.swing.JLabel jLestadoCivil;
    private javax.swing.JLabel jLfechaNacimiento;
    private javax.swing.JLabel jLlogin;
    private javax.swing.JLabel jLnombre;
    private javax.swing.JLabel jLpais;
    private javax.swing.JLabel jLsexo;
    private javax.swing.JPasswordField jPFclave1;
    private javax.swing.JPasswordField jPFclave2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTFapellido;
    private javax.swing.JTextField jTFcedula;
    private javax.swing.JTextField jTFlogin;
    private javax.swing.JTextField jTFnombre;
    // End of variables declaration//GEN-END:variables

}
